package com.jdls.service;

import com.jdls.mapper.StudentMapper;
import com.jdls.pojo.Student;
import com.jdls.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

/**
 * @author 嘉德罗斯
 * @date 2021/9/1
 */
public class StudentService {
    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
    public List<Student> select(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> students = mapper.select();
        sqlSession.close();
        return students;
    }

    public void add(Student student){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        mapper.add(student);
        sqlSession.commit();
        sqlSession.close();
    }

    public void delete(String id){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        mapper.delete(id);
        sqlSession.commit();
        sqlSession.close();
    }

    public Student selectByID(String id){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = mapper.selectByID(id);
        sqlSession.close();
        return student;
    }

    public void update(Student student){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        mapper.update(student);
        sqlSession.commit();
        sqlSession.close();
    }
}
